<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>IP Check Files</title>
    <meta http-equiv="refresh" content="10"> <!-- 页面每 10 秒自动刷新 -->
</head>
<body>
    <h1>IP Check Files with Content 1</h1>
    <table border="1">
        <thead>
        <tr>
            <th>IP Address</th>
            <th>File Link</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        <?php
        $ip_check_dir = 'ip_check';
        $ip_value_dir = 'ip_value';
        if (is_dir($ip_check_dir)) {
            $files = scandir($ip_check_dir);
            foreach ($files as $file) {
                if ($file!= '.' && $file!= '..') {
                    $file_path = $ip_check_dir. '/'. $file;
                    try {
                        $content = file_get_contents($file_path);
                        if (trim($content) == '1') {
                            $ip = str_replace('.txt', '', $file);
                            $ip_value_file_path = $ip_value_dir. '/'. $ip. '.txt';
                            echo '<tr>';
                            echo '<td>'. $ip. '</td>';
                            echo '<td><a href="'. $ip_value_file_path. '">'. $ip. '</a></td>';
                            echo '<td><form action="" method="post"><input type="hidden" name="file" value="'. $file_path. '"><input type="submit" value="Block IP"></form></td>';
                            echo '</tr>';
                        }
                    } catch (Exception $e) {
                        echo '<tr><td colspan="3">Error reading '. $file. ': '. $e->getMessage(). '</td></tr>';
                    }
                }
            }
        } else {
            echo '<tr><td colspan="3">ip_check directory does not exist</td></tr>';
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['file'])) {
            $file_path = $_POST['file'];
            try {
                $file = fopen($file_path, 'w');
                if ($file) {
                    fwrite($file, '2');
                    fclose($file);
                    // 刷新页面
                    echo '<script>window.location.reload();</script>';
                } else {
                    echo 'Error opening file for writing';
                }
            } catch (Exception $e) {
                echo 'Error updating file: '. $e->getMessage();
            }
        }
       ?>
        </tbody>
    </table>
</body>
</html>